package com.github.exception;

import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 全局异常处理器
 */
@ControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理Shiro权限异常
     */
    @ExceptionHandler({UnauthorizedException.class})
    public void handleUnauthorizedException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        System.out.println("1111捕获到权限异常：" + e.getMessage());
        // 重定向到未授权页面
        response.sendRedirect(request.getContextPath() + "/noauth");
    }
    @ExceptionHandler({AuthorizationException.class})
    public void handleAuthorizationException(HttpServletRequest request, HttpServletResponse response, Exception e) throws IOException {
        System.out.println("2222捕获到权限异常：" + e.getMessage());
        // 重定向到未授权页面
        response.sendRedirect(request.getContextPath() + "/noauth");
    }
}